package com.ypbase.cloud.tags.controller;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.framework.core.exception.AppLayerException;
import com.cloud.framework.core.log.LoggerFactory;
import com.ypbase.cloud.tags.api.ITagApi;
import com.ypbase.cloud.tags.api.dto.TagDto;
import com.ypbase.cloud.tags.common.WebResponse;

@RequestMapping("tag")
@Controller
public class TagController {

	/**
	 * the logger
	 */
	private static final Logger logger = LoggerFactory.getLogger(TagController.class);

	@Autowired
	private ITagApi tagApi;

	/**
	 * 分页条件查询
	 * 
	 * @param companyId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getTagPageList", method = RequestMethod.POST)
	public WebResponse getTagPageList(@RequestBody TagDto tagDto,
			@RequestParam(value = "pageNo", required = false, defaultValue = "1") Long pageNo,
			@RequestParam(value = "pageSize", required = false, defaultValue = "10") Long pageSize) {

		WebResponse response = new WebResponse(null);
		try {
			response.setData(tagApi.getTagPageList(tagDto,pageNo,pageSize));

		} catch (Throwable e) {
			logger.error("异常", e);
			response.setStatus("-1");
			response.setMessage("信息异常" + e.getMessage());
		}
		return response;
	}

	/**
	 * 添加标签
	 * 
	 * @param tagDto
	 * @return
	 * @throws AppLayerException
	 */
	@ResponseBody
	@RequestMapping(value = "/addTag", method = RequestMethod.POST)
	public WebResponse addTag(@RequestBody TagDto tagDto) {
		WebResponse response = new WebResponse(null);
		try {
			tagApi.addTag(tagDto);
		} catch (Throwable e) {
			logger.error("异常", e);
			response.setStatus("-1");
			response.setMessage("信息异常" + e.getMessage());
		}
		return response;
	}

	/**
	 * 编辑标签
	 * 
	 * @param tagDto
	 * @return
	 * @throws AppLayerException
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/updateTag", method = RequestMethod.POST)
	public WebResponse updateCompany(@RequestBody TagDto tagDto) {
		WebResponse response = new WebResponse(null);
		try {
			tagApi.updateTag(tagDto);
		} catch (Throwable e) {
			logger.error("异常", e);
			response.setStatus("-1");
			response.setMessage("信息异常" + e.getMessage());
		}
		return response;
	}

	/**
	 * 删除标签
	 * 
	 * @param tagDto
	 * @return
	 * @throws AppLayerException
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/deleteTag", method = RequestMethod.POST)
	public WebResponse deleteTag(@RequestParam(value = "id") Long id) {
		WebResponse response = new WebResponse(null);
		try {
			tagApi.deleteTagById(id);
		} catch (Throwable e) {
			logger.error("异常", e);
			response.setStatus("-1");
			response.setMessage("信息异常" + e.getMessage());
		}
		return response;
	}
}
